package com.wl4g.sample.callgo.sample1;

import static java.lang.String.format;
import static java.lang.String.valueOf;
import static java.lang.System.currentTimeMillis;
import static java.lang.System.getProperty;
import static java.lang.System.out;

import java.io.IOException;

public class App1 {

	static {
		// System.load(getProperty("user.dir").concat("/src/main/resources/sample1/libsample1.so"));
		System.load(getProperty("user.dir").concat("/src/main/resources/sample1/libsample1.dll"));
	}

	public static void main(String[] args) throws Exception {
		sampleCallTest1();

		long runCount = 100_0000L;
		performanceWithNativeTest2(runCount);
		performanceContrastWithJavaTest3(runCount);
	}

	private static void sampleCallTest1() {
		out.println("Method to call int type parameter of golang ...");
		out.println(LibSample1.INSTANCE.Sum(111, 222));
	}

	@SuppressWarnings("unused")
	private static void performanceWithNativeTest2(long run_count) throws IOException {
		long begin = currentTimeMillis();
		for (int i = 0; i < run_count; i++) {
			int sum = LibSample1.INSTANCE.Sum(222, 333);
		}
		out.println(format("Running time of native implementation Cost: %sms", valueOf(currentTimeMillis() - begin)));
	}

	@SuppressWarnings("unused")
	private static void performanceContrastWithJavaTest3(long run_count) throws IOException {
		long begin = currentTimeMillis();
		for (int i = 0; i < run_count; i++) {
			int sum = Sum(222, 333);
		}
		out.println(format("Running time of pure java implementation Cost: %sms", valueOf(currentTimeMillis() - begin)));
	}

	private static int Sum(int a, int b) {
		return a + b;
	}

}